#include "login_roles.h"
#include <algorithm>

// 验证手机号格式
bool LoginModule::validatePhone(const std::string& phone) const {
    if(phone.length() == 11)
    {
        if(phone.find_first_not_of("0123456789") == std::string::npos)
        {
                return true;
        }
    }
    return false;
}

// 验证密码格式
bool LoginModule::validatePassword(const std::string& password) const {
    return password.length() >= 3;
}

// 登录功能
bool LoginModule::login(const std::string& phone, const std::string& password, User& user) {
    if (!validatePhone(phone) || !validatePassword(password)) {
        std::cerr << "Invalid phone or password format." << std::endl;
        return false;
    }

    for (const auto& u : users) 
    {
        if (u.getPhone() == phone && u.getPassword() == password) {
            user = u;
            std::cout << "Login successful. Welcome, " << user.getNickname() << "!" << std::endl;
            return true;
        }
    }

    std::cerr << "Login failed. Invalid phone or password." << std::endl;
    return false;
}

// 注册功能
bool LoginModule::registerUser(const std::string& phone, const std::string& password, const std::string& nickname) {
    if (!validatePhone(phone)) {
        std::cerr << "Invalid phone format." << std::endl;
        return false;
    }
    if (!validatePassword(password)) {
        std::cerr << "Invalid password format." << std::endl;
        return false;
    }

    for (const auto& u: users) {
        if (u.getPhone() == phone) {
            std::cerr << "Phone number already registered." << std::endl;
            return false;
        }
    }

    int new_user_id = users.empty() ? 1 : users.back().getUserID() + 1;
    users.emplace_back(new_user_id, phone, password, CONSUMER, nickname);
    std::cout << "Registration successful. Welcome, " << nickname << "!" << std::endl;
    return true;
}

// 修改密码功能
bool LoginModule::changePassword(const std::string& phone, const std::string& oldPassword, const std::string& newPassword) {
    if (!validatePhone(phone)) {
        std::cerr << "Invalid phone format." << std::endl;
        return false;
    }
    if (!validatePassword(oldPassword)) {
        std::cerr << "Invalid old password format." << std::endl;
        return false;
    }
    if (!validatePassword(newPassword)) {
        std::cerr << "Invalid new password format." << std::endl;
        return false;
    }

    for (auto& u: users) {
        if (u.getPhone() == phone && u.getPassword() == oldPassword) {
            u.setPassword(newPassword);
            std::cout << "Password changed successfully." << std::endl;
            return true;
        }
    }

    std::cerr << "Password change failed. Invalid phone or old password." << std::endl;
    return false;
}

// 修改手机号功能
bool LoginModule::changePhone(const std::string& oldPhone, const std::string& newPhone) {
    if (!validatePhone(oldPhone)) {
        std::cerr << "Invalid old phone format." << std::endl;
        return false;
    }
    if (!validatePhone(newPhone)) {
        std::cerr << "Invalid new phone format." << std::endl;
        return false;
    }

    for (auto& u: users) {
        if (u.getPhone() == oldPhone) {
            u.setPhone(newPhone);
            std::cout << "Phone number changed successfully." << std::endl;
            return true;
        }
    }

    std::cerr << "Phone number change failed. Invalid old phone number." << std::endl;
    return false;
}